package managedbean.SalesBP;

import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import managedbean.Workspace.FileUploadManagerBean;
import merlion.crm.slcm.entity.ContactRecord;
import merlion.crm.sopm.entity.SalesOrder;
import merlion.frm.bpm.entity.Invoice;
import merlion.frm.bpm.session.SalesBPSessionBeanLocal;
import merlion.scm.lcm.entity.DeliveryOrder;

/**
 *
 * @author Kenn
 */
@ManagedBean(name = "sbpInvoiceManagerBean")
@ViewScoped
public class SalesInvoiceManagerBean {

    @EJB
    public SalesBPSessionBeanLocal sbpSessionBean;
    @ManagedProperty(value = "#{fileUploadManagerBean}")
    private FileUploadManagerBean fileManagerBean;
    private Invoice invoice;
    private SalesOrder salesOrder;
    private String statusMsg;
    private Collection<Invoice> invoices;

    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------
    public SalesInvoiceManagerBean() {
        invoice = (Invoice) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("invoice");
        statusMsg = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("statusMsg");
        
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("invoice");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("statusMsg");
        
        if(invoice == null){
            invoice = new Invoice();
            salesOrder = new SalesOrder();
        }
    }

    @PostConstruct
    private void init() {
        
    }

    //--------------------------------------------------------------------------
    //                     Invoice Manager Bean Methods
    //--------------------------------------------------------------------------
    public String createInvoice(Long id) {

        DeliveryOrder deliveryOrder = sbpSessionBean.getDeliveryOrderById(id);
        invoice = sbpSessionBean.createInvoice(deliveryOrder);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("invoice", invoice);

        return "createInvoiceConfirm?faces-redirect=true";
    }

    public String saveInvoice() {

        invoice = sbpSessionBean.saveInvoice(invoice);
        if (invoice != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Sales Invoice created successfully.");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Transaction to create sales invoice has failed. Please try again.");
        }
        return "viewInvoiceStatus?faces-redirect=true";
    }

    public String cancelSalesInvoice() {
        
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Create Sales Invoice cancelled.");
        return "viewInvoiceStatus?faces-redirect=true";
    }
    
    public String viewSalesInvoice(){
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("invoice", invoice);
        return "viewInvoiceDisplay?faces-redirect=true";
    }
    
//    public String closeSalesInvoice(){
//        //setStatusMsg("Sale invoice closed.");
//        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Sales Invoice closed.");
//        return "viewInvoiceStatus?faces-redirect=true";
//    }

    //--------------------------------------------------------------------------
    //                          Getter and Setter
    //--------------------------------------------------------------------------
    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public SalesOrder getSalesOrder() {
        return salesOrder;
    }

    public void setSalesOrder(SalesOrder salesOrder) {
        this.salesOrder = salesOrder;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public Collection<Invoice> getInvoices() {
        return sbpSessionBean.getAllInvoice();
    }

    public void setInvoices(Collection<Invoice> invoices) {
        this.invoices = invoices;
    }
    
    // send email , edited by zy Nov 9
    public String sendInvoiceToCustomer() {
        // edited by ZY, 9 Nov 2011
        String res = "invoice sent to supplier";
        String emailAddress = "";
        
        if (invoice == null || invoice.getCustomer() == null || invoice.getCustomer().getContactRecords() == null || invoice.getCustomer().getContactRecords().isEmpty()){
            res = "invocie is not able to sent to customer because no contact record is found.";
            System.out.println(res);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Sent unsuccessfully", "sent unsuccessfully"));
        } else {
            emailAddress = new ArrayList<ContactRecord>(invoice.getCustomer().getContactRecords()).get(0).getEmail();
            fileManagerBean.sendInvoice(emailAddress, invoice.getId());
            System.out.println("email address: "+emailAddress);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sent successfully", "sent successfully"));
        }
        return null;
    }
    
    public FileUploadManagerBean getFileManagerBean() {
        return fileManagerBean;
    }

    public void setFileManagerBean(FileUploadManagerBean fileManagerBean) {
        this.fileManagerBean = fileManagerBean;
    }
}
